using DG.Tweening;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HomeSkins : MonoBehaviour
{
	[Header("Home")]
	public RectTransform buttonTransfrom;

	[Header("Popup")]
	public GameObject skinsPopupObject;

	public RectTransform skinContentTransform;

	public ScrollRect scrollView;

	public HomeSkinSlot slotPrefab;

	public GameObject selectSkinButton;

	public GameObject buySkinButton;

	public GameObject newSkinNotificationObject;

	private List<HomeSkinSlot> _slotList = new List<HomeSkinSlot>();

	private int _currentSelectedSkin;

	private UIInApp buySkinInApp;

	private Tween _displayTween;

	private bool _canClosePopup;

	private void Awake()
	{
		skinsPopupObject.SetActive(value: false);
	}

	private void Start()
	{
		float width = slotPrefab.GetComponent<RectTransform>().rect.width;
		foreach (SkinParameters skin in SkinManager.Instance.Skins)
		{
			if (skin.eventName == SkinManager.EventName.None || PlayerData.Instance.IsSkinUnlocked(skin.skinIndex))
			{
				HomeSkinSlot homeSkinSlot = UnityEngine.Object.Instantiate(slotPrefab, slotPrefab.transform.parent);
				homeSkinSlot.gameObject.SetActive(value: true);
				homeSkinSlot.SetParameters(skin);
				Achievement achievementForSkin = AchievementManager.Instance.GetAchievementForSkin(skin.skinIndex);
				if ((bool)achievementForSkin)
				{
					homeSkinSlot.SetAchievement(achievementForSkin);
				}
				_slotList.Add(homeSkinSlot);
			}
		}
		for (int i = 0; i < _slotList.Count; i++)
		{
			float num = (0f - width) * (float)(_slotList.Count - 1) / 2f + (float)i * width;
			_slotList[i].SetXPosition(Mathf.RoundToInt(0f - num));
		}
		SnapScrollView component = scrollView.GetComponent<SnapScrollView>();
		component.OnElementHighlighted.AddListener(OnSkinHighlighted);
		newSkinNotificationObject.SetActive(App.Instance.hasUnlockedSkin);
		buySkinInApp = buySkinButton.GetComponent<UIInApp>();
	}

	public void DisplayWithAnimation(bool state)
	{
		if (_displayTween != null)
		{
			_displayTween.Kill();
		}
		if (state)
		{
			_displayTween = buttonTransfrom.DOAnchorPosX(0f, 0.2f).SetEase(Ease.OutBack);
		}
		else
		{
			_displayTween = buttonTransfrom.DOAnchorPosX(-120f, 0.2f);
		}
	}

	private void OnSkinHighlighted(int index)
	{
		_currentSelectedSkin = index;
		for (int i = 0; i < _slotList.Count; i++)
		{
			bool flag = i == index;
			HomeSkinSlot homeSkinSlot = _slotList[i];
			homeSkinSlot.SetZoom(flag);
			if (flag)
			{
				SkinParameters currentParameters = homeSkinSlot.CurrentParameters;
				bool flag2 = PlayerData.Instance.IsSkinUnlocked(currentParameters.skinIndex);
				selectSkinButton.SetActive(flag2);
				buySkinButton.SetActive(!flag2);
				buySkinInApp.ProductId = currentParameters.storeID;
			}
		}
	}

	public void CALLBACK_BUTTON_OpenSkins()
	{
		_canClosePopup = false;
		skinsPopupObject.SetActive(value: true);
		for (int i = 0; i < _slotList.Count; i++)
		{
			HomeSkinSlot homeSkinSlot = _slotList[i];
			bool flag = homeSkinSlot.CurrentParameters.skinIndex == App.Instance.currentSkinIndex;
			homeSkinSlot.SetSelected(flag);
			homeSkinSlot.SetUnlocked(PlayerData.Instance.IsSkinUnlocked(homeSkinSlot.CurrentParameters.skinIndex));
			if (flag)
			{
				scrollView.content.anchoredPosition = new Vector3(homeSkinSlot.XPosition, 0f);
				OnSkinHighlighted(i);
			}
		}
		if (App.Instance.hasUnlockedSkin)
		{
			App.Instance.hasUnlockedSkin = false;
			newSkinNotificationObject.SetActive(App.Instance.hasUnlockedSkin);
		}
		skinContentTransform.localScale = new Vector3(1f, 0f, 1f);
		skinContentTransform.DOScaleY(1f, 0.2f).SetDelay(0.2f).OnComplete(delegate
		{
			_canClosePopup = true;
		});
		HomeManager.Instance.DisplayHomeUI(state: false);
	}

	public void CALLBACK_BUTTON_CloseSkins()
	{
		if (_canClosePopup)
		{
			
			skinContentTransform.localScale = new Vector3(1f, 1f, 1f);
			skinContentTransform.DOScaleY(0f, 0.2f).OnComplete(delegate
			{
				skinsPopupObject.SetActive(value: false);
				HomeManager.Instance.DisplayHomeUI(state: true);
			});
            _canClosePopup = false;
        }
	}

	public void CALLBACK_BUTTON_SelectSkin()
	{
		App.Instance.currentSkinIndex = _slotList[_currentSelectedSkin].CurrentParameters.skinIndex;
		HomeManager.Instance.OnChangeSkin();
		for (int i = 0; i < _slotList.Count; i++)
		{
			HomeSkinSlot homeSkinSlot = _slotList[i];
			bool flag = homeSkinSlot.CurrentParameters.skinIndex == App.Instance.currentSkinIndex;
			homeSkinSlot.SetSelected(flag);
			if (flag)
			{
				scrollView.content.DOKill();
				scrollView.content.DOAnchorPosX(homeSkinSlot.XPosition, 0.2f);
				OnSkinHighlighted(i);
			}
		}
	}

	public void OnSkinPurchaseComplete(string id)
	{
		foreach (HomeSkinSlot slot in _slotList)
		{
			if (slot.CurrentParameters.storeID == id)
			{
				slot.SetUnlocked(state: true);
				selectSkinButton.SetActive(value: true);
				buySkinButton.SetActive(value: false);
				VoodooServiceLocator.Analytics.LogBuySkin(slot.CurrentParameters.skinIndex);
			}
		}
	}
}
